<?php
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2016/12/28 00:40
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class SocialRelationShip_Model extends WX_Model
{
    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @param $userid
     * @param int $level
     * @return mixed
     */
    public function getFansListsByUserId($userid, $level = 1)
    {
        if ($level == 1) {
            $where['puserid'] = $userid;
        } else {
            $where['ppuserid'] = $userid;
        }

        $db = clone($this->db);
        $q = $db->where($where)
            ->select('u.nickname, u.headimgurl, u.created')
            ->from('user_wx_relationship as r')
            ->join('user_wx as u', 'u.userid = r.userid')
            ->get();

        return $q->result_array();
    }

    /**
     * 一级粉丝数量
     * @param $userid
     */
    public function getFansCountByUserId_Level_1($userid)
    {
        $where['puserid'] = $userid;
        $db = clone($this->db);
        $db->where($where);
        $db->from('user_wx_relationship');
        return $db->count_all_results();
    }

    /**
     * 二级粉丝数量
     * @param $userid
     */
    public function getFansCountByUserId_Level_2($userid)
    {
        $where['ppuserid'] = $userid;
        $db = clone($this->db);
        $db->where($where);
        $db->from('user_wx_relationship');
        return $db->count_all_results();
    }

    /**
     * 获取一条记录
     * @param $userid
     * @return mixed
     */
    public function getRelationItem($userid)
    {
        $db = clone($this->db);
        $query = $db->get_where('user_wx_relationship', ['userid' => $userid]);
        return $query->row_array();
    }

    /**
     * 设置一条记录
     * @param $userid
     * @param int $puserid
     * @param int $ppuserid
     * @return mixed
     */
    public function setRelationItem($userid, $puserid = 0, $ppuserid = 0)
    {
        $db = clone($this->db);
        $db->insert('user_wx_relationship', [
            'userid' => $userid,
            'puserid' => $puserid,
            'ppuserid' => $ppuserid
        ]);
        return $db->insert_id();
    }
}